package cn.ztuo.bitrade.controller;


import cn.ztuo.bitrade.entity.ContractOrder;
import cn.ztuo.bitrade.entity.ContractWallet;
import cn.ztuo.bitrade.entity.transform.AuthMember;
import cn.ztuo.bitrade.service.ContractOrderService;
import cn.ztuo.bitrade.service.ContractWalletService;
import cn.ztuo.bitrade.service.MemberService;
import cn.ztuo.bitrade.util.MessageResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.SessionAttribute;

import java.util.List;

import static cn.ztuo.bitrade.constant.SysConstant.SESSION_MEMBER;

@Slf4j
@RestController
@RequestMapping("/contract-wallet")
public class ContractWalletController extends BaseController {

    @Autowired
    private ContractWalletService contractWalletService;
    @Autowired
    private ContractOrderService contractOrderService;
    @Autowired
    private MemberService memberService;
    /**
     * 合约账户资产
     * @param member
     * @return
     */
    @RequestMapping(value = "/get",method = RequestMethod.POST)
    public MessageResult getUserContractWallet(@SessionAttribute(SESSION_MEMBER) AuthMember member){
        log.info("---------查询用户合约账户:" + member.getId());
        List<ContractWallet> result = contractWalletService.findAllByMemberId(member.getId());
        return success(result);
    }

    /**
     * 获取合约订单接口
     * @param member
     * @param orderId
     * @param status
     * @param pageNo
     * @param pageSize
     * @return
     */
    @RequestMapping(value = "/order",method = RequestMethod.POST)
    public Page<ContractOrder> getUserContractOrder(@SessionAttribute(SESSION_MEMBER) AuthMember member, String orderId,String status, int pageNo, int pageSize){
        Page<ContractOrder> page = contractOrderService.findHistory(member.getId(),orderId,status,pageNo,pageSize);
        page.getContent().forEach(contractOrder -> {
            contractOrder.setUsername(memberService.findUserNameById(contractOrder.getMemberId()));
        });
        return page;
    }
}
